#!/usr/bin/python
import threading, thread, time
import gobject, gtk

gtk.gdk.threads_init()

class Pygtk_Thread(object):
   def __init__(self, update_function, complete_callback,complete_callback_args=None):
       self.update_function = update_function
       self.complete_callback = complete_callback
       self.complete_callback_args = complete_callback_args
       
   def _start(self,*args):
       self._stopped = False
       self.update_function(*args)
       if self._stopped:
           thread.exit()
       if self.complete_callback_args is not None:
           gobject.idle_add(self.complete_callback,self.complete_callback_args)
       else:
           gobject.idle_add(self.complete_callback)
   
   def start(self, *args):
       threading.Thread(target=self._start, args=args).start()

   def stop(self):
       self._stopped = True